package com.njym.java.security.admin.rest;

import com.njym.java.security.admin.biz.TravelBussinessRelationBiz;
import com.njym.java.security.admin.biz.TravelDistanceBiz;
import com.njym.java.security.admin.entity.TravelBussinessRelation;
import com.njym.java.security.admin.entity.TravelBussinessRelationVo;
import com.njym.java.security.admin.entity.TravelDistance;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("travelDistance")
public class TravelDistanceController extends BaseController<TravelDistanceBiz,TravelDistance> {

    @Autowired
    private TravelBussinessRelationBiz travelBussinessRelationBiz;

    @RequestMapping(value = "/addObjs", method = RequestMethod.GET)
    @ResponseBody
    public Object addObjs(@RequestParam Map<String, Object> data) throws ParseException {
        try {
            String travelName = data.get("travelName").toString();
            String place = data.get("place").toString();
            String brief = data.get("brief").toString();
            String time = data.get("timePoint").toString();
            Date timePoint = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
            List<Integer> bussinessId = new ArrayList<>();
            if (!data.get("bussId").toString().equals("")) {
                List<String> ids = Arrays.asList(data.get("bussId").toString().split(","));
                ids.forEach( id -> {
                    bussinessId.add(Integer.parseInt(id));
                });
            }

            TravelDistance entity = new TravelDistance();
            entity.setTimePoint(timePoint);
            entity.setTravelName(travelName);
            entity.setPlace(place);
            entity.setBrief(brief);
            baseBiz.insertSelective(entity);

            bussinessId.forEach(item ->{
                TravelBussinessRelation relation = new TravelBussinessRelation();
                relation.setBussinessId(item);
                relation.setTravelId(entity.getId());
                relation.setModiffyTime(new Date());
                travelBussinessRelationBiz.insertSelective(relation);
            });
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            System.out.println(e);
            return new ObjectRestResponse<>().rel(false);
        }
    }

    @RequestMapping(value = "/getObj", method = RequestMethod.GET)
    @ResponseBody
    public Object getObj(int id){
        TravelDistance distance = baseBiz.selectById(id);
        List<TravelBussinessRelationVo> list = travelBussinessRelationBiz.selectRelaticeBussiness(id);
        Map<String,Object> data = new HashMap<>();
        data.put("distance",distance);
        data.put("list",list);
        return ResponseUtil.ok(data);
    }

    @RequestMapping(value = "/deleteObj", method = RequestMethod.GET)
    @ResponseBody
    public Object deleteObj(int id){
        try {
            baseBiz.deleteById(id);
            TravelBussinessRelation entity = new TravelBussinessRelation();
            entity.setTravelId(id);
            travelBussinessRelationBiz.delete(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            System.out.println(e);
            return new ObjectRestResponse<>().rel(false);
        }
    }

    @RequestMapping(value = "/putObj", method = RequestMethod.GET)
    @ResponseBody
    public Object putObj(@RequestParam Map<String, Object> data) throws ParseException {
        try {
            List<Integer> bussinessId = new ArrayList<>();
            if (!data.get("bussId").toString().equals("")) {
                List<String> ids = Arrays.asList(data.get("bussId").toString().split(","));
                ids.forEach( id -> {
                    bussinessId.add(Integer.parseInt(id));  //新的
                });
            }

            Integer id = Integer.parseInt(data.get("id").toString());
            String travelName = data.get("travelName").toString();
            String place = data.get("place").toString();
            String brief = data.get("brief").toString();
            String time = data.get("timePoint").toString();
            Date timePoint = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);

            List<Integer> the_ids = travelBussinessRelationBiz.selectIds(id);   //旧的
            List<Integer> list_1 = new ArrayList<>();   //要删除的
            List<Integer> list_2 = new ArrayList<>();   //要保存的
            the_ids.forEach( item ->{
                if ( !bussinessId.contains(item)) {
                    list_1.add(item);
                }
            });
            bussinessId.forEach( item ->{
                if ( !the_ids.contains(item) ) {
                    list_2.add(item);
                }
            });

            list_1.forEach( item-> {
                TravelBussinessRelation entity = new TravelBussinessRelation();
                entity.setTravelId(id);
                entity.setBussinessId(item);
                travelBussinessRelationBiz.delete(entity);
            });
            list_2.forEach( item ->{
                TravelBussinessRelation entity = new TravelBussinessRelation();
                entity.setTravelId(id);
                entity.setBussinessId(item);
                entity.setModiffyTime(new Date());
                travelBussinessRelationBiz.insertSelective(entity);
            });

            TravelDistance travelDistance = new TravelDistance();
            travelDistance.setId(id);
            travelDistance.setPlace(place);
            travelDistance.setBrief(brief);
            travelDistance.setTravelName(travelName);
            travelDistance.setTimePoint(timePoint);
            baseBiz.updateSelectiveById(travelDistance);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            System.out.println(e);
            return new ObjectRestResponse<>().rel(false);
        }

    }


}